<?php

/**
 * This is the model class for table "tbl_tutor".
 *
 * The followings are the available columns in table 'tbl_tutor':
 * @property string $id
 * @property string $tutor_name
 * @property string $image_path
 * @property string $home_number
 * @property string $street
 * @property string $id_district
 * @property string $birthday
 * @property integer $sex
 * @property string $phone
 * @property string $email
 * @property string $is_teacher
 * @property string $school
 * @property string $branch
 * @property integer $want_teacher_course
 * @property string $school_fees
 * @property integer $is_arrange
 * @property string $other_request
 * @property integer $status
 * @property string $date1
 * @property string $date2
 * @property string $date3
 * @property string $date4
 * @property string $date5
 * @property string $date6
 * @property string $date7
 * @property string $date8
 * @property string $date9
 * @property string $date10
 * @property string $date11
 * @property string $date12
 * @property string $date13
 * @property string $date14
 * @property string $date15
 * @property string $date16
 * @property string $date17
 * @property string $date18
 * @property string $date19
 * @property string $date20
 * @property string $date21
 */
class Tutor extends CActiveRecord
{
        public $image;
        public $subjectIds = array();
        public $districtIds = array();

    /**
	 * Returns the static model of the specified AR class.
	 * @return Tutor the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'tbl_tutor';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('tutor_name, home_number, street, id_district, birthday, sex, phone, is_teacher, want_teacher_course, school_fees, is_arrange', 'required'),
			array('sex, want_teacher_course, is_arrange, status', 'numerical', 'integerOnly'=>true),
			array('tutor_name', 'length', 'max'=>200),
			array('home_number, id_district, date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11, date12, date13, date14, date15, date16, date17, date18, date19, date20, date21', 'length', 'max'=>10),
			array('street, other_request', 'length', 'max'=>250),
			array('phone, school_fees', 'length', 'max'=>20),
			array('email, is_teacher, school, branch', 'length', 'max'=>50),
                        array('image', 'file', 'types'=>'jpg, gif, png'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, tutor_name, home_number, street, id_district, birthday, sex, phone, email, is_teacher, school, branch, want_teacher_course, school_fees, is_arrange, other_request, status, date1, date2, date3, date4, date5, date6, date7, date8, date9, date10, date11, date12, date13, date14, date15, date16, date17, date18, date19, date20, date21', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'districts' => array(self::MANY_MANY, 'District', 'tbl_tutor_district(id_tutor, id_district)'),
			'subjects' => array(self::MANY_MANY, 'Subject', 'tbl_tutor_subject(id_tutor, id_subject)'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'tutor_name' => 'Tên gia sư',
                        'image_path' => 'Hình ảnh',
			'home_number' => 'Số nhà',
			'street' => 'Đường',
			'id_district' => 'Quận',
			'birthday' => 'Ngày sinh',
			'sex' => 'Giới tính',
			'phone' => 'Điện thoại',
			'email' => 'Email',
			'is_teacher' => 'Trình độ',
			'school' => 'Trường',
			'branch' => 'Chuyên ngành',
			'want_teacher_course' => 'Muốn lớp giáo viên',
			'school_fees' => 'Lương đề nghị',
			'is_arrange' => 'Có thể sắp xếp thời gian',
			'other_request' => 'Yêu cầu khác',
			'status' => 'Status',
			'date1' => 'Date1',
			'date2' => 'Date2',
			'date3' => 'Date3',
			'date4' => 'Date4',
			'date5' => 'Date5',
			'date6' => 'Date6',
			'date7' => 'Date7',
			'date8' => 'Date8',
			'date9' => 'Date9',
			'date10' => 'Date10',
			'date11' => 'Date11',
			'date12' => 'Date12',
			'date13' => 'Date13',
			'date14' => 'Date14',
			'date15' => 'Date15',
			'date16' => 'Date16',
			'date17' => 'Date17',
			'date18' => 'Date18',
			'date19' => 'Date19',
			'date20' => 'Date20',
			'date21' => 'Date21',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id,true);

		$criteria->compare('tutor_name',$this->tutor_name,true);

		$criteria->compare('home_number',$this->home_number,true);

		$criteria->compare('street',$this->street,true);

		$criteria->compare('id_district',$this->id_district,true);

		$criteria->compare('birthday',$this->birthday,true);

		$criteria->compare('sex',$this->sex);

		$criteria->compare('phone',$this->phone,true);

		$criteria->compare('email',$this->email,true);

		$criteria->compare('is_teacher',$this->is_teacher,true);

		$criteria->compare('school',$this->school,true);

		$criteria->compare('branch',$this->branch,true);

		$criteria->compare('want_teacher_course',$this->want_teacher_course);

		$criteria->compare('school_fees',$this->school_fees,true);

		$criteria->compare('is_arrange',$this->is_arrange);

		$criteria->compare('other_request',$this->other_request,true);

		$criteria->compare('status',$this->status);

		$criteria->compare('date1',$this->date1,true);

		$criteria->compare('date2',$this->date2,true);

		$criteria->compare('date3',$this->date3,true);

		$criteria->compare('date4',$this->date4,true);

		$criteria->compare('date5',$this->date5,true);

		$criteria->compare('date6',$this->date6,true);

		$criteria->compare('date7',$this->date7,true);

		$criteria->compare('date8',$this->date8,true);

		$criteria->compare('date9',$this->date9,true);

		$criteria->compare('date10',$this->date10,true);

		$criteria->compare('date11',$this->date11,true);

		$criteria->compare('date12',$this->date12,true);

		$criteria->compare('date13',$this->date13,true);

		$criteria->compare('date14',$this->date14,true);

		$criteria->compare('date15',$this->date15,true);

		$criteria->compare('date16',$this->date16,true);

		$criteria->compare('date17',$this->date17,true);

		$criteria->compare('date18',$this->date18,true);

		$criteria->compare('date19',$this->date19,true);

		$criteria->compare('date20',$this->date20,true);

		$criteria->compare('date21',$this->date21,true);

		return new CActiveDataProvider('Tutor', array(
			'criteria'=>$criteria,
		));
	}
       protected function afterSave() {
        parent::afterSave();
        $oldSubjects = $this->subjects;
        $oldDistricts = $this->districts;

        if (is_array($oldSubjects) && count($oldSubjects)) {
            $oldSubjects = CHtml::listData($this->subjects, 'id', 'id');
        } else {
            $oldSubjects = array();
        }

        if (is_array($oldDistricts) && count($oldDistricts)) {
            $oldDistricts = CHtml::listData($this->districts, 'id', 'id');
        } else {
            $oldDistricts = array();
        }
        
        if (is_array($this->subjectIds) && count($this->subjectIds))
        {
            $deleteSubjects = array_diff($oldSubjects, $this->subjectIds);
            $newSubjects = array_diff($this->subjectIds, $oldSubjects);
            $db = $this->getDbConnection();
            if (is_array($newSubjects)&&count($newSubjects))
            {

                foreach ($newSubjects as $subjectId)
                {
                    $command = $db->createCommand('INSERT INTO tbl_tutor_subject(id_tutor, id_subject) VALUES(:id_tutor, :id_subject)');
                    $command->bindValue(':id_subject', $subjectId);
                    $command->bindValue(':id_tutor', $this->id);
                    $command->execute();
                }
            }
        } elseif (count($oldSubjects))
        {
            //delete all old authors
            $deleteSubjects = $oldSubjects;
        }
         else {
            $deleteSubjects = array();
        }

        if (is_array($this->districtIds) && count($this->districtIds))
        {
            $deleteDistricts = array_diff($oldDistricts, $this->districtIds);
            $newDistricts = array_diff($this->districtIds, $oldDistricts);
            $db = $this->getDbConnection();
            if (is_array($newDistricts)&&count($newDistricts))
            {

                foreach ($newDistricts as $districtId)
                {
                    $command = $db->createCommand('INSERT INTO tbl_tutor_district(id_tutor, id_district) VALUES(:id_tutor, :id_district)');
                    $command->bindValue(':id_district', $districtId);
                    $command->bindValue(':id_tutor', $this->id);
                    $command->execute();
                }
            }
        } elseif (count($oldDistricts))
        {
            //delete all old authors
            $deleteDistricts = $oldDistricts;
        }
         else {
            $deleteDistricts = array();
        }
        //delete
        if (count($deleteSubjects))
        {
            $db = $this->getDbConnection();
            foreach ($deleteSubjects as $subjectId)
            {
                $command = $db->createCommand('DELETE FROM tbl_tutor_subject WHERE id_tutor=:id_tutor AND id_subject=:id_subject');
                $command->bindValue(':id_tutor', $this->id);
                $command->bindValue(':id_subject', $subjectId);
                $command->execute();
            }
        }
        if (count($deleteDistricts))
        {
            $db = $this->getDbConnection();
            foreach ($deleteDistricts as $districtId)
            {
                $command = $db->createCommand('DELETE FROM tbl_tutor_district WHERE id_tutor=:id_tutor AND id_district=:id_district');
                $command->bindValue(':id_tutor', $this->id);
                $command->bindValue(':id_district', $districtId);
                $command->execute();
            }
        }
    }

    protected function afterFind() {
        $this->subjectIds = $this->subjects;
        if (is_array($this->subjectIds) && count($this->subjectIds)) {
            $this->subjectIds = CHtml::listData($this->subjectIds, 'id', 'id');
        } else {
            $this->subjectIds = array();
        }
        $this->districtIds = $this->districts;
        if (is_array($this->districtIds) && count($this->districtIds)) {
            $this->districtIds = CHtml::listData($this->districtIds, 'id', 'id');
        } else {
            $this->districtIds = array();
        }
    }

    public function getSubjectNames()
    {
        $subjects = array();
        if (is_array($this->subjects) && count($this->subjects))
        {
            $subjects = CHtml::listData($this->subjects, 'id', 'name');
            return implode(', ', $subjects);
        }
        return '';
    }
    public function getDistrictNames()
    {
        $districts = array();
        if (is_array($this->districts) && count($this->districts))
        {
            $districts = CHtml::listData($this->districts, 'id', 'name');
            return implode(', ', $districts);
        }
        return '';
    }
    public function getSubjects() {
        $subjects = "";
        foreach ($this->subjects as $subject) {
                $subjects.= ' '.CHtml::encode($subject->name);
            }
        return $subjects;
    }
    public function getDistricts() {
        $districts = "";
        foreach ($this->districts as $district) {
                $districts.= ' '.CHtml::encode($district->name);
            }
        return $districts;
    }
    public function getSexName() {
        if($this->sex == 1) {
            return "Nam";
        } else {
            return "Nữ";
        }
    }
    public function getStatusName() {
        if($this->status == 1) {
            return "Có";
        } else {
            return "Chưa";
        }
    }
}